package com.cennavi.mineplay.mineplay_data.model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;

public class TableCreateTask extends Thread {
	
	
	public static String getTodayTableName(){
		return "gps_"+new SimpleDateFormat("yyyyMMdd").format(new Date());
	}
	

	private int days;
	private JdbcTemplate jdbc;
	
	public TableCreateTask(int days,JdbcTemplate jdbc){
		this.days = days;
		this.jdbc = jdbc;
	}

	@Override
	public void run() {

		while(true){
			this.task();
			
			try {
				Thread.sleep(24 * 3600 * 1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	private void task(){
		String sql = "select max(substring(tablename,5)) as max_gps from pg_tables where schemaname='public' and length(tablename) = 12 and tablename ~ 'gps_[0-9]+'";
		
		 Map<String,Object> map = jdbc.queryForMap(sql);
		
		int max_day = -1;
		
//		if (list.size()>0){
//			max_day = Integer.parseInt(list.get(0).get("max_gps").toString());
//		}
		
		if (map.get("max_gps")!= null)
			max_day = Integer.parseInt(map.get("max_gps").toString());
		
//		else{
//			max_day = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date())) + 1;
//		}
		
		int today = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
		
		if (max_day != -1){
			while (max_day - today < days - 1){
				
				try {
					long dlong = new SimpleDateFormat("yyyyMMdd").parse(String.valueOf(max_day)).getTime() + (24 * 3600 * 1000);
				
					String fix = new SimpleDateFormat("yyyyMMdd").format(new Date(dlong));
					String tableName = "gps_"+ fix;
					this.createTable(tableName);
					
					max_day = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date(dlong)));
					
				} catch (ParseException e) {
					e.printStackTrace();
				}
				
				
				
			}
		}else{
			long time = new Date().getTime();
			for(int i=0;i<10;i++){
				String fix = new SimpleDateFormat("yyyyMMdd").format(new Date(time + (i * 24 * 3600 * 1000)));
				String tableName = "gps_"+ fix;
				this.createTable(tableName);
				
			}
		}
	}
	
	private void createTable(String tableName){
		//创建表
		String createTableSql = "CREATE TABLE public." + tableName +
								"("+
								  "  car_number character varying(100),"+
								  "  company_id integer,"+
								  "  geom geometry,"+
								  "  speed double precision,"+
								  "  direct double precision,"+
								  "  time bigint NOT NULL,"+
								  "  is_online integer" +
								  " )";
		jdbc.execute(createTableSql);
		
		//创建空间索引
		String createSpatialSql = "CREATE INDEX idx_"+tableName+"_geom "+
								  "  ON public."+tableName+" USING gist "+
								  "  (geom)";
		jdbc.execute(createSpatialSql);
		
		//创建主键
		String sqlPrimaryKeySql = "alter table public."+tableName+" add constraint pk_"+tableName+" primary key(car_number,time)";
		
		jdbc.execute(sqlPrimaryKeySql);
	}
}
